package com.easy.service.impl;

import com.easy.bean.Detail;
import com.easy.bean.Order;
import com.easy.dao.IDetailDao;
import com.easy.dao.IOrderDao;
import com.easy.service.IDetailService;
import com.easy.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class DetailServiceImpl implements IDetailService {

    @Autowired
    IDetailDao dao;

    @Autowired
    IOrderDao orderDao;


    @Override
    public Detail getDeliveryDetailByID(int id) {
        return dao.getDeliveryDetailByID(id);
    }
    @Override
    public Detail getReceiptDetailByID(int id) {
        return dao.getReceiptDetailByID(id);
    }

    @Override
    public List<Detail> getDeliveryDetail(Detail detail, Page page) {
        return dao.getDeliveryDetail(detail,page);
    }
    @Override
    public List<Detail> getReceiptDetail(Detail detail, Page page) {
        return dao.getReceiptDetail(detail,page);
    }

    @Override
    public int getDeliveryCount(Detail detail) {
        return dao.getDeliveryCount(detail);
    }
    @Override
    public int getReceiptCount(Detail detail) {
        return dao.getReceiptCount(detail);
    }




    @Override
    public int addDeliveryDetail(Detail detail) {
        return dao.addDeliveryDetail(detail);
    }
    @Override
    public int addReceiptDetail(Detail detail) {
        return dao.addReceiptDetail(detail);
    }

    @Override
    @Transactional
    public int delDeliveryDetail(int id) {
        Detail detail = dao.getDeliveryDetailByID(id);
        Order order = orderDao.getOrderByNumber(detail.getTrackingNumber());
        if (order.getStatus().equals("运输中")){
            order.setStatus("待处理");
        }
        orderDao.editOrder(order);
        return dao.delDeliveryDetail(id);
    }
    @Override
    @Transactional
    public int delReceiptDetail(int id) {
        Detail detail = dao.getDeliveryDetailByID(id);
        Order order = orderDao.getOrderByNumber(detail.getTrackingNumber());
        if (order.getStatus().equals("已签收")){
            order.setStatus("已送达");
        }
        orderDao.editOrder(order);
        return dao.delReceiptDetail(id);
    }

}
